package com.taptech.doufu.ui.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.HeaderViewListAdapter;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.WeiboUserBean;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.services.personalcenter.WeiboService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.personalcenter.WeiboUserAdapter;
import com.taptech.doufu.ui.view.personalcenter.WeiboMessageWindow;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.util.multipleImageSelector.FileUtils;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnLoadAndRefreshListener;
/**
 * 
 <p>
 * Company: DiaoBao
 * </p>
 * 
 * 
 @author huangzh
 * 
 * 
 @date 2014-3-20
 */
public class PersonalInviteWeiboContactActivity extends DiaobaoBaseActivity
		implements HttpResponseListener, OnLoadAndRefreshListener {

	private PullToRefreshListView weiboContactList;
	WeiboUserAdapter adapter;
	int pageIndex, groupId;
	public String inviteCode, groupName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_center_activity_invite_weibo_contact);
		groupId = getIntent().getIntExtra(Constant.GROUP_ID, -1);
		groupName = getIntent().getStringExtra(Constant.GROUP_NAME);
		initView();
//		loadWeiboContact();
	}

	private void initView() {
		weiboContactList = (PullToRefreshListView) findViewById(R.id.personal_center_invite_weibo_list);
		adapter = new WeiboUserAdapter();

		weiboContactList.setOnLoadAndRefreshListener(this);
		weiboContactList.setAdapter(adapter);
		weiboContactList.setRefreshable(false);
		weiboContactList.setLoadmoreable(true);
		weiboContactList.setClickable(false);
	}

	@Override
	public void handleResponse(int handleType, HttpResponseObject response) {
		try {
			switch (handleType) {
			case PersonalInfoService.HANDLE_GET_INVITE_CODE:
				if (response.getStatus() == 0) {
					JSONObject json = (JSONObject) response.getData();
					inviteCode = json.getString(Constant.INVITATION_CODE);
					// UIUtil.toastMessage(
					// this,
					// "邀请码:"
					// + json.get(Constant.INVITATION_CODE)
					// + "::有效期"
					// + DiaobaoUtil.seconds2Date(Long.parseLong(json
					// .getString(Constant.INVITE_END))));

					showWindow();
				} else {
					UIUtil.toastMessage(this,
							DiaobaoUtil.getErrorTips(response));
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void loadWeiboContact() {
//		WeiboService.getInstance().getSinaWeiboFriendsList(
//				new RequestListener() {
//
//					@Override
//					public void onComplete(String arg0) {
//						try {
//							final JSONObject json = new JSONObject(arg0);
//							final JSONArray friends = (JSONArray) json.get("users");
//
//							runOnUiThread(new Runnable() {
//								public void run() {
//									try {
//										if (json.has("next_cursor")) {
//											if (pageIndex == json
//													.getInt("next_cursor"))
//												weiboContactList.setFull(true);
//											else
//												pageIndex = json
//														.getInt("next_cursor");
//										}
//										adapter.setSuperDatas(DiaobaoUtil
//												.jsonArray2BeanList(
//														WeiboUserBean.class,
//														friends));
//										weiboContactList.loadMoreComplete();
//									} catch (Exception e) {
//										e.printStackTrace();
//									}
//
//								}
//							});
//						} catch (JSONException e) {
//							e.printStackTrace();
//						}
//
//					}
//
//					@Override
//					public void onWeiboException(WeiboException e) {
//
//					}
//				}, pageIndex);
	}

	WeiboMessageWindow window;

	public void invite(View view) {

		if (getSelectedList().size() == 0) {
			UIUtil.toastMessage(this, "您还未选中好友");
			return;
		}

		if (inviteCode != null) {
			showWindow();
		} else {
			PersonalInfoService.getInstance()
					.getInviteCode(groupId, this, this);
		}

	}

	private void showWindow() {
		if (window == null) {
			window = new WeiboMessageWindow(this);
		} else {
			if (!window.isShowing()) {
				window.showAtLocation(getWindow().getDecorView(),
						Gravity.CENTER, 0, 0);
			}
		}

	}

	private List<WeiboUserBean> getSelectedList() {
		List<Object> list;
		if (weiboContactList.getAdapter() instanceof HeaderViewListAdapter) {
			HeaderViewListAdapter listAdapter = (HeaderViewListAdapter) weiboContactList
					.getAdapter();
			list = ((WeiboUserAdapter) listAdapter.getWrappedAdapter())
					.getDataSource();
		} else {
			list = ((WeiboUserAdapter) weiboContactList.getAdapter())
					.getDataSource();
		}

		List<WeiboUserBean> returnList = new ArrayList<WeiboUserBean>();
		for (Object object : list) {
			if (((WeiboUserBean) object).isSelected()) {
				returnList.add((WeiboUserBean) object);
			}
		}
		return returnList;

	}

	public void sendWeiboMessage(View view) {
		File file=new File(Constant.AppDir.DIR_CACHE_IMAGE+ "diaobao_test.png");
	    if(!file.exists()){
	    	Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.new_icon);
	    	FileUtils.saveBitmap(bmp);
	    	bmp.recycle();
	    }
//		for (WeiboUserBean object : getSelectedList()) {
//			if (object.isSelected()) {
//				WeiboService.getInstance().sendWeiboMessage(
//						object.getName(), inviteCode,
//						groupName, new RequestListener() {
//
//							@Override
//							public void onComplete(String arg0) {
//								runOnUiThread(new Runnable() {
//									public void run() {
//										window.dismiss();
//										UIUtil.toastMessage(
//												PersonalInviteWeiboContactActivity.this,
//												"发送邀请成功");
//									}
//								});
//
//							}
//
//							@Override
//							public void onWeiboException(WeiboException e) {
//
//							}
//						});
//			}
//
//		}
	}

	public void cancel(View view) {
		if (window != null)
			window.dismiss();
	}

	public void back(View view) {
		onBackPressed();
	}

	@Override
	public void onRefresh() {

	}

	@Override
	public void loadMore() {
		loadWeiboContact();
	}

}
